package Region.socket;

import Region.DatabaseManager;
import Region.LogManager;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.sql.SQLException;

public class CopyThread implements Runnable{
    public BufferedReader input = null;
    public PrintWriter output = null;
    private DatabaseManager dataBaseManager;
    private LogManager logManager;
    private MasterSocketManager masterSocketManager;
    Socket socket;
    @Override
    public void run(){
        try {
            String command = input.readLine();
            //解析表名和SQL语句
            int table_index = command.indexOf(',');
            String tableName = command.substring(0,table_index);
            String sql = command.substring(table_index+1);
            System.out.println("执行指令："+sql);
            dataBaseManager.updateSQL(sql);
            logManager.addLog(tableName, sql);

        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public CopyThread(Socket socket, MasterSocketManager masterSocketManager, DatabaseManager dataBaseManager, LogManager logManager) throws IOException {
        this.masterSocketManager = masterSocketManager;
        this.socket = socket;
        this.logManager = logManager;
        this.dataBaseManager = dataBaseManager;

        this.input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        this.output = new PrintWriter(socket.getOutputStream());
    }
}
